package utils.audit;

import org.hibernate.cfg.Configuration;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.metamodel.source.MetadataImplementor;
import org.hibernate.service.spi.SessionFactoryServiceRegistry;

/**
 * Created by FireFire on 4/5 0005.
 * 事件监听器注册
 */
public class AuditIntegrator implements Integrator{

    @Override
    public void integrate(
            Configuration configuration,
            SessionFactoryImplementor sessionFactory,
            SessionFactoryServiceRegistry serviceRegistry) {

        EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );

        listenerRegistry.appendListeners( EventType.PRE_INSERT, new PreInsertEventListenerImpl() );
        listenerRegistry.appendListeners( EventType.PRE_UPDATE, new PreUpdateEventListenerImpl());
        listenerRegistry.appendListeners( EventType.POST_INSERT, new PostInsertEventListenerImpl( ) );
        listenerRegistry.appendListeners( EventType.POST_UPDATE, new PostUpdateEventListenerImpl( ) );
    }

    @Override
    public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {

    }

    /**
     * {@inheritDoc}
     * @see Integrator#integrate(MetadataImplementor, SessionFactoryImplementor, SessionFactoryServiceRegistry)
     */
    @Override
    public void integrate( MetadataImplementor metadata,
                           SessionFactoryImplementor sessionFactory,
                           SessionFactoryServiceRegistry serviceRegistry ) {
    }
}
